﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class ClaimTypeRepository : BaseRepository
    {
        public ClaimTypeRepository()
            : base()
        {
        }

        public ClaimTypeRepository(SecurityApiEntities context)
            :base(context)
        {
        }

        public IQueryable<ClaimType> GetClaimTypes(int applicationId = 1)
        {
            var query = from o in _context.ClaimTypes
                        where o.ApplicationId == applicationId
                        select o;
            return query;
        }

        public ClaimType GetClaimTypeById(int id)
        {
            var query = from o in _context.ClaimTypes
                        where o.ClaimTypeId == id
                        select o;
            return query.SingleOrDefault();
        }

        public ClaimType GetClaimType(string name, string uri, int applicationId = 1)
        {
            name = name.Trim().ToLower();
            uri = uri.Trim().ToLower();
            var query = _context.ClaimTypes.Where(p => p.Name == name && p.Uri == uri && p.ApplicationId == applicationId);
            return query.SingleOrDefault();
        }

        public bool AddClaimType(string name, string uri, int applicationId = 1)
        {
            name = name.Trim().ToLower();
            uri = uri.Trim().ToLower();

            bool bResult = true;

            try
            {
                ClaimType resource = new ClaimType();
                resource.Name = name;
                resource.Uri = uri;
                resource.ApplicationId = applicationId;
                resource.adt_DateCreated = DateTime.Now;
                _context.ClaimTypes.Add(resource);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public bool RemoveClaimType(int claimTypeId)
        {
            bool bResult = true;

            try
            {
                ClaimType claimType = _context.ClaimTypes.Where(p => p.ClaimTypeId == claimTypeId).SingleOrDefault();

                if (null != claimType)
                {
                    _context.ClaimTypes.Remove(claimType);
                    _context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

        public bool RemoveClaimType(string name, string uri, int applicationId = 1)
        {
            bool bResult = true;
            name = name.Trim().ToLower();
            uri = uri.Trim().ToLower();
            try
            {
                ClaimType claimType = _context.ClaimTypes.Where(p => p.Name == name && p.Uri == uri && p.ApplicationId == applicationId).SingleOrDefault();

                if (null != claimType)
                {
                    _context.ClaimTypes.Remove(claimType);
                    _context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }


    }
}
